//
//  BKIntegralBookViewController.m
//  bestkeep
//
//  Created by yons on 16/12/5.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKIntegralBookViewController.h"
#import <NJKWebViewProgress.h>
#import <NJKWebViewProgressView.h>
#import "BKMainCommon.h"


@interface BKIntegralBookViewController ()<UIWebViewDelegate,NJKWebViewProgressDelegate>

@property(nonatomic,strong)UIWebView *webView;
@property(nonatomic,strong)NJKWebViewProgress *progressProxy;
@property(nonatomic,strong)NJKWebViewProgressView *progressView;
@end

@implementation BKIntegralBookViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadWebView];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    _progressView.hidden = YES;
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

}

-(void)loadWebView{
    
    _progressProxy = [[NJKWebViewProgress alloc] init];
    _progressProxy.webViewProxyDelegate = self;
    _progressProxy.progressDelegate = self;
    
    CGRect navBounds = self.navigationController.navigationBar.bounds;
    CGRect barFrame = CGRectMake(0, navBounds.size.height - 2, navBounds.size.width, 2);
    _progressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];
    _progressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    [_progressView setProgress:0 animated:YES];
    
    [self.navigationController.navigationBar addSubview:_progressView];
    
    _webView = [[UIWebView alloc] init];
    _webView.delegate = _progressProxy;
    [self.view addSubview:_webView];
    
    [_webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top);
        make.left.equalTo(self.view.mas_left);
        make.bottom.equalTo(self.view.mas_bottom);
        make.right.equalTo(self.view.mas_right);
    }];
    NSURL *url = [NSURL URLWithString:self.urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    [_webView loadRequest:request];
    
    NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary];
    NSString* versionNum =[infoDict objectForKey:@"CFBundleVersion"];
    NSString *version = [NSString stringWithFormat:@"%@.%@",[infoDict objectForKey:@"CFBundleShortVersionString"],versionNum];
    NSString *appName = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleNameKey];
    if (appName == nil) {
        appName =@"";
    }
    NSString *deviceModel = [[UIDevice currentDevice] model];
    NSString *deviceSys = [[UIDevice currentDevice] systemVersion];
    CGFloat screenScale = [[UIScreen mainScreen] scale];
    NSString *user_agent = [NSString stringWithFormat:@"%@/%@ (%@; iOS %@; Scale/%0.2f)",appName,version,deviceModel,deviceSys,screenScale];
    [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent":user_agent}];
    
    
    
    
    
    
}

- (void)didFinishLoadHtml{
    
    
    [_webView stringByEvaluatingJavaScriptFromString: @"OBJC.executeJs(\"init\",\"{}\")"];
    
    
}

-(void)didFailLoadHtml:(NSError *)error{
    
    if (error) {
        [BKIndicationView showInView:self.view];
        NSURL *url =[[NSURL alloc]initWithString:self.urlString];
        NSURLRequest *request =[[NSURLRequest alloc]initWithURL:url];
        [self.webView loadRequest:request];
        [BKIndicationView dismiss];
    }
}

#pragma mark - UIWebViewDelegate
//结束加载时调用的方法
-(void)webViewDidFinishLoad:(UIWebView *)webView {//html加载完后触发
    
    [self didFinishLoadHtml];
    NSString * str_title=[webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    self.title=str_title;
    
    NSLog(@"结束加载时调用的方法:%@--%@",str_title, [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.innerHTML"] );
    
    
}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    
    [self didFailLoadHtml:error];
    
}

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    NSString *requestString = [[request URL]absoluteString];//获取请求的绝对路径.
    requestString = [requestString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    return YES;
    
}
#pragma mark - NJKWebViewProgressDelegate
-(void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress
{
    [_progressView setProgress:progress animated:YES];
    if (!self.title) {
        self.title = [_webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
